<?php
namespace ChannelThrift\Controller;
include 'WeaverAuctionTestCase.class.php';

use \Think\PhpUnit;
use \Thrift\Protocol\TBinaryProtocol;
use \Thrift\Protocol\TMultiplexedProtocol;
use \Thrift\Transport\THttpClient;
use \Thrift\Transport\TBufferedTransport;
use \Thrift\Exception\TException;

global $isClean;
$isClean = true;

/**
 * 保证金服务单元测试;
    使用示例:
    phpunit ../Tests/BailControllerTest.class.php
    phpunit ../Tests/BailControllerTest.class.php --group GetBail        
    phpunit ../Tests/BailControllerTest.class.php --group IncBail        
    phpunit ../Tests/BailControllerTest.class.php --group Freeze  
    phpunit ../Tests/BailControllerTest.class.php --group UnFreeze 
    phpunit ../Tests/BailControllerTest.class.php --group DecAndUnFreeze    
    phpunit ../Tests/BailControllerTest.class.php --group DecBail

 * @requires extension mysqli 
 * @requires extension redis
 * @requires extension PHP 5.4.45 
 */
class BidControllerTest extends WeaverAuctionTestCase
{

    private $client = null;
    private $redis = null;
    private $dealerID = 467;
    private $orderID = 2521;
    private $redisDatabase = 0;
    
    /**
        @param serviceName 服务模块名
        @param servicePath 服务路径
        @param serviceDomain 服务域名
        @param servicePort 服务端口
        @return thriftclient 
    */
    private function getThriftClient($serviceName,$servicePath,$serviceDomain,$servicePort=80)
    {      
        if (!$serviceName){
            //抛出异常            
            return false;
        }
        $handle = new THttpClient($serviceDomain, $servicePort, $servicePath);
        $transport = new TBufferedTransport($handle);
        $protocol = new TBinaryProtocol($transport);
        $auctionProtocol = new TMultiplexedProtocol($protocol, $serviceName);
        $serviceName='\\Services\\'.$serviceName.'\\'.$serviceName.'Client';
        $client = new $serviceName($auctionProtocol);
        return $client;        
    }

    private function getRedis(){        
        $config = require ROOT_PATH.'Application/ChannelThrift/Conf/redis.php';        
        $redis = new \Redis();
        $redis->connect($config['REDIS_HOST'],$config['REDIS_PORT']);
        $redis->select($this->redisDatabase);
        return $redis;
    }

    /**
        @before 
    */
    public function testThriftClient(){
        $client = $this->getThriftClient('BailService','/Bail/run','songaimin.auction.weaver.s.isou365.cn'); 
        $this->client = $client;
        $this->redis = $this->getRedis();
    }
    /**
        清理测试的数据
        @afterClass
        @group clean 
    */
    public function testClean(){
        global $isClean;
        if ($isClean){
            echo __METHOD__ ."\n";
            $this->redis->del('AuctionWeaver:OrderBid:Queue');
            $this->redis->del('AuctionWeaver:OrderBid:Summary:SpecVar:'.$this->orderID);
        }
    }


}
